#include"OneWayWall.h"
#include"LSYEasyBody.h"
#include"LSYEasyWorld.h"
#include"BallMover.h"
#include"OneWallWayConatctListener.h"
#include"Tool.h"

USING_NS_CC;

bool OneWayWall::init()
{
	if (!AbstractLayer::init(b2Vec2(0, -10)))
	{
		return false;
	}
	return true;
}

void OneWayWall::box2DAppReady()
{
	auto size = Director::getInstance()->getWinSize();
	LSYEasyBody::createRectangle(b2Vec2(0, 0), size.width, size.height);
	ground = LSYEasyBody::createBox(b2Vec2(size.width*0.5, 40), b2Vec2(size.width*0.5, 80), b2BodyType::b2_kinematicBody);
	ground->SetUserData((void*)&USER_DATA_GROUND);
	
	player = LSYEasyBody::createCircle(b2Vec2(50, 250), 20);
	player->SetUserData((void*)&USER_DATA_PLAYER);
	player->SetSleepingAllowed(false);

	platfrom = LSYEasyBody::createBox(b2Vec2(300, 250), b2Vec2(50, 5), b2BodyType::b2_kinematicBody);
	platfrom->SetUserData((void*)&USER_DATA_FLATFROM);
	platfrom = LSYEasyBody::createBox(b2Vec2(350, 250), b2Vec2(50, 5), b2BodyType::b2_kinematicBody);
	platfrom->SetUserData((void*)&USER_DATA_FLATFROM);
	platfrom = LSYEasyBody::createBox(b2Vec2(150,200), b2Vec2(50, 5), b2BodyType::b2_kinematicBody);
	platfrom->SetUserData((void*)&USER_DATA_FLATFROM);
	platfrom->SetTransform(platfrom->GetPosition(), PI / 4);
	platfrom = LSYEasyBody::createBox(b2Vec2(100, 200), b2Vec2(50, 5), b2BodyType::b2_kinematicBody);
	platfrom->SetUserData((void*)&USER_DATA_FLATFROM);
	
	ballMover = new BallMover(player);
	
	listener = new OneWallWayContactsListener(this);
	pWorld->SetContactListener(listener);
}

void OneWayWall::update(float dt)
{
	AbstractLayer::update(dt);

	ballMover->isReadyJump = listener->isPlayerOnGround;
	ballMover->update();
}

void OneWayWall::onKeyPressed(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event * event)
{
	ballMover->onKeyDown((int)keyCode);
}

void OneWayWall::onKeyReleased(cocos2d::EventKeyboard::KeyCode keyCode, cocos2d::Event * event)
{
	ballMover->onKeyUp((int)keyCode);
}
